package com.qf.md.unit.controller;

import com.qf.common.PageParam;
import com.qf.common.PageVO;
import com.qf.common.R;
import com.qf.entity.md.unit.MdUnitMeasure;
import com.qf.md.unit.param.UnitSearchParam;
import com.qf.md.unit.service.MdUnitMeasureService;
import com.sun.deploy.net.HttpResponse;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * ==== 天道酬勤 ====
 *
 * @author douzhouzhou
 * @date 2025/2/27
 * <p>
 * ==== 行稳致远 进而有为 ====
 */
@CrossOrigin
@RestController
@RequestMapping("/md/unit")
@Api(tags = "计量单位管理")
@Slf4j
public class MdUnitMeasureController {
    @Resource
    private MdUnitMeasureService mdUnitMeasureService;
    @GetMapping("/list")
    public R<PageVO<MdUnitMeasure>> unitPage(UnitSearchParam unitSearchParam) {
        PageVO<MdUnitMeasure> MdUnitMeasurePageVO = mdUnitMeasureService.unitPage(unitSearchParam);
        return R.ok(MdUnitMeasurePageVO);
    }
    @PostMapping("/save")
    public R<MdUnitMeasure> saveMdUnitMeasure(@RequestBody MdUnitMeasure mdUnitMeasure) {
        //log.info("保存计量单位信息:{}",mdUnitMeasure);
//        log.info("info");
//        log.warn("warn");
//        log.error("error");
//        log.debug("debug");
        int i = mdUnitMeasureService.saveMdUnitMeasure(mdUnitMeasure);
        if (i > 0){
            return R.ok(mdUnitMeasure);
        }
        return R.fail("保存失败");
    }
    @GetMapping("/info")
    public R<MdUnitMeasure> findById(Long id) {
        MdUnitMeasure mdUnitMeasure = mdUnitMeasureService.findById(id);
        return R.ok(mdUnitMeasure);
    }
    @PostMapping("/update")
    public R<MdUnitMeasure> updateMdUnitMeasure(@RequestBody MdUnitMeasure mdUnitMeasure) {
        int i = mdUnitMeasureService.updateMdUnitMeasure(mdUnitMeasure);
        if (i > 0){
            return R.ok(mdUnitMeasure);
        }
        return R.fail("保存失败");
    }
    @GetMapping("/primaryUnit")
    public R<List<MdUnitMeasure>> findByPrimaryUnit() {
        List<MdUnitMeasure> byPrimaryUnit = mdUnitMeasureService.findByPrimaryUnit();
        return R.ok(byPrimaryUnit);
    }
    @PostMapping("/deleteById")
    public R<String> deleteById(@RequestParam Long id) {
        int i = mdUnitMeasureService.deleteById(id);
        if (i > 0){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
    @PostMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestBody List<Long> ids) {
        mdUnitMeasureService.deleteMdUnitMeasureBatch(ids);

            return R.ok("删除成功");

    }
    //导入接口
    @PostMapping("/upload/excel")
    public R uploadExcel(MultipartFile file){
        System.out.println(file);
        mdUnitMeasureService.uploadExcel(file);
        return R.ok("导入成功");
    }

    @GetMapping("/download/excel")
    public R downloadExcel(HttpServletResponse response) {
        mdUnitMeasureService.downloadExcel(response);
        return R.ok("下载成功");
    }
}
